<?php

namespace App\Controller\Api\Wuliu;

use App\Base\BaseController4Restful;
use App\Entity\Wuliu\WuliuGoodsType;
use App\Entity\Wuliu\WuliuOrder;
use App\Repository\Wuliu\WuliuCarsRepository;
use App\Repository\Wuliu\WuliuCarTypeRepository;
use App\Repository\Wuliu\WuliuGoodsTypeRepository;
use App\Repository\Wuliu\WuliuLoadTypeRepository;
use App\Repository\Wuliu\WuliuOrderRepository;
use App\Repository\Wuliu\WuliuUserRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiWuliuOrderController extends BaseController4Restful
{
    /**
     * @Route("/api/wuliuOrders", name="apiGetWuliuOrders",methods={"GET","POST"})
     */
    public function apiGetWuliuOrders(WuliuOrderRepository $repository,Request $request,WuliuUserRepository $wuliuUserRepository)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $WuliuOrder WuliuOrder*/
                $WuliuOrder = $this->createEntity($data,WuliuOrder::class);
                $this->myEm()->persist($WuliuOrder);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $WuliuOrder);
            default:
                $user = $this->needToBeUser();
                $wuser = $wuliuUserRepository->getByUser($user);
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count(["user"=>$wuser]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array("user"=>$wuser),array("id"=>"DESC"),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/wuliuOrders/{id}", name="apiAddWuliuOrders",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateWuliuOrders(WuliuOrder $WuliuOrder,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($WuliuOrder);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $WuliuOrder WuliuOrder*/
                $WuliuOrder = $this->updateEntity($data,WuliuOrder::class,$WuliuOrder);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($WuliuOrder);$this->myEm()->flush();

        $this->setGroups("order");

        return $this->json_out(200,"获取成功", $WuliuOrder);
    }
    /**
     * @Route("/api/create_order", name="apiCreateWuliuOrder",methods={"GET","PUT","PATCH","POST"})
     */
    public function apiCreateWuliuOrder(
        WuliuUserRepository $wuliuUserRepository,
        WuliuCarsRepository $wuliuCarsRepository,
        WuliuGoodsTypeRepository $wuliuGoodsTypeRepository,
        WuliuLoadTypeRepository $wuliuLoadTypeRepository,
        WuliuCarTypeRepository $wuliuCarTypeRepository
    )
    {
        $data = $this->getRequestData();
        $user = $this->needToBeUser();
        $wUser = $wuliuUserRepository->getByUser($user);
        $order = new WuliuOrder();
        $order->setStatus(0)
            ->setUser($wUser)

            ->setTitle($data['title'])

            ->setCar($wuliuCarsRepository->find($data["car"]))
            ->setGoods($wuliuGoodsTypeRepository->find($data["goods"]))
            ->setLoadType($wuliuLoadTypeRepository->find($data["load"]))
            ->setCarType($wuliuCarTypeRepository->find($data["carType"]))

            ->setStartLat($data['startLat'])
            ->setStartLon($data['startLon'])
            ->setStartAddress($data['startAddress'])

            ->setEndLat($data['endLat'])
            ->setEndLon($data['endLon'])
            ->setEndAddress($data['endAddress'])

            ->setContact($data['contact'])
            ->setPhone($data['phone'])
            ->setPrice(0)
            ->setPriceWill($data['price'])
            ->setTips($data['tips'])

            ->setCreateAt(new \DateTime())

            ->setSn(createOrderSn($user->getId()));

        $this->myEm()->persist($order);
        $this->myEm()->flush();

        return $this->json_out(1,"创建成功",$order);

    }

}
